﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DimensionData.Data2
{
    public class PathExpression
    {
        private string[] codes;

        private PathExpression()
        {
        }

        public PathExpression(string path)
        {
            codes = path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static bool operator ==(PathExpression p1, PathExpression p2)
        {
            if (p1.codes.Length == p2.codes.Length)
            {
                bool isMatch = true;
                for (int index = 0; index < p1.codes.Length; index++)
                {
                    if ((p1.codes[index] != "*" && p2.codes[index] != "*" && p1.codes[index] != p2.codes[index]))
                    {
                        isMatch = false;
                        break;
                    }
                }
                return isMatch;
            }
            return false;
        }

        public static bool operator !=(PathExpression p1, PathExpression p2)
        {
            return !(p1 == p2);
        }

        /// <summary>
        /// 由一个具体的路径把表达式定位到一个具体的路径位置
        /// </summary>
        /// <param name="pathExp"></param>
        /// <returns></returns>
        public PathExpression Locate(PathExpression pathExp)
        {
            PathExpression newExp = pathExp.Clone();

            if (this.codes.Length != pathExp.codes.Length)
            {
                throw new Exception("维度表达式格式不匹配，无法执行操作");
            }

            for (int index = 0; index < this.codes.Length; index++)
            {
                string segement = newExp.codes[index];
                if (segement == "*")
                {
                    newExp.codes[index] = this.codes[index];
                }
            }
            return newExp;
        }

        public PathExpression Clone()
        {
            PathExpression clonedItem = new PathExpression();
            clonedItem.codes = new string[this.codes.Length];
            for (int index = 0; index < this.codes.Length; index++)
            {
                clonedItem.codes[index] = this.codes[index];
            }
            return clonedItem;
        }

        /// <summary>
        /// 输出路径文本串
        /// </summary>
        /// <returns></returns>
        public string ToPathString()
        {
            StringBuilder pathBuilder = new StringBuilder();
            for (int index = 0; index < this.codes.Length; index++)
            {
                pathBuilder.AppendFormat(@"\{0}", this.codes[index]);
            }
            return pathBuilder.ToString();
        }

        /// <summary>
        /// 比较两个表达式是否一样，
        /// -1表示不一样
        /// 返回值越大代表越接近
        /// 越靠近后面的维度码匹配的时候，获得的维度权值越大
        /// </summary>
        /// <param name="targetExp"></param>
        /// <returns></returns>
        public int Compare(PathExpression targetExp)
        {
            int result = 0;
            if (this.codes.Length == targetExp.codes.Length)
            {
                for (int index = 0; index < this.codes.Length; index++)
                {
                    if (this.codes[index] == "*" || targetExp.codes[index] == "*")
                    {
                        result += 0;
                    }
                    else if (this.codes[index] == targetExp.codes[index])
                    {
                        result = result | (1 << index);
                    }
                    else
                    {
                        result = - 1;
                        break;
                    }
                }
            }
            return result;
        }
    }
}
